<template>
  <!-- 修改购买人信息组件 -->
  <el-dialog width="30%" v-el-drag-dialog title="购买人信息" :visible.sync="visible">
    <div v-loading="loading" element-loading-text="保存中...">
      <el-form class="mar-l-10 mar-r-10" ref="form" :rules="rules" :model="entity" label-position="right"
        label-width="140px" style="margin-bottom: 50px;">
        <el-form-item label="购买人姓名：" prop="certificateRealName">
          <el-input type="textarea" :autosize="{ minRows: 1, maxRows: 2}" placeholder=""
            v-model="entity.certificateRealName">
          </el-input>
        </el-form-item>
        <el-form-item label="购买人证件类型：" prop="certificateType">
          <el-select v-model="entity.certificateType" placeholder="请选择证件类型" clearable>
            <el-option v-for="(val, key) in dict.certificateType" v-if="key === '1'" :key="key" :label="val"
              :value="key">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="购买人证件号：" prop="certificateNumber">
          <el-input type="textarea" :autosize="{ minRows: 1, maxRows: 2}" placeholder=""
            v-model="entity.certificateNumber">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSave">保存</el-button>
        <el-button @click="handleCancel">取消</el-button>
      </div>
    </div>
  </el-dialog>
</template>
<script>
  import {
    getDictByGroupName
  } from '@/api/common/common'
  import {
    doUpdateCerrificateInfo
  } from '@/api/goods/order'
  import {
    getToken
  } from '@/utils/auth'
  import elDragDialog from '@/directive/el-dragDialog'

  export default {
    props: {
      onSuccess: Function
    },
    directives: {
      elDragDialog
    },
    watch: {},
    data() {
      var checkIdentitytionId = (rule, value, callback) => {
        var errorMsg = this.checkIsIdentityId(value)
        if (errorMsg !== '') {
          callback(new Error(errorMsg))
        } else {
          callback()
        }
      }
      return {
        loading: false,
        entity: {
          orderId: '',
          certificateRealName: '',
          certificateNumber: '',
          certificateType: ''
        },
        // 数据字典
        dict: {
          certificateType: {}
        },
        visible: false,
        rules: {
          certificateType: [{
            required: true,
            message: '请选择证件类型',
            trigger: 'change'
          }],
          certificateRealName: [{
            required: true,
            message: '请填写真实姓名',
            trigger: 'blur'
          }],
          certificateNumber: [{
            required: true,
            validator: checkIdentitytionId,
            trigger: 'blur'
          }]
        }
      }
    },
    created() {
      this.token = {
        accessToken: getToken()
      }
      // 获取数据字典
      getDictByGroupName('ProjectOrderDTO').then(response => {
        this.dict = response.data
      })
    },
    methods: {
      handleSave() {
        var that = this
        that.entity.enterpriseId = that.getEnterpriseId()
        that.$refs['form'].validate((valid) => {
          if (valid) {
            that.$confirm('确定要修改购买人信息吗？', '温馨提示', {
              distinguishCancelAndClose: true,
              confirmButtonText: '确定',
              cancelButtonText: '取消'
            }).then(() => {
              that.loading = true
              doUpdateCerrificateInfo(that.entity).then(response => {
                that.loading = false
                if (response.isError) return
                that.msgSuccess(response.msg)
                that.onSuccess(that.entity)
                that.handleCancel()
              })
            }).catch(action => {})
          }
        })
      },
      handleCancel() {
        this.visible = false
        this.entity.certificateRealName = ''
        this.entity.certificateType = ''
        this.entity.certificateNumber = ''
      },
      open(orderInfo) {
        if (!orderInfo) {
          this.msgError('请选择要操作的订单!')
        }
        orderInfo.certificateType = orderInfo.certificateType ? orderInfo.certificateType.toString() : '99'
        this.entity = orderInfo
        this.visible = true
      }
    }
  }
</script>

<style lang="less" scoped>

</style>
